<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\xueao\controller;

use controller\XueaoAdmin;
use service\DataService;
use service\ToolsService;
use think\Db;

/**
 * Class MyCate 自定义分类
 * @package app\jujin\controller
 */
class MyCate extends XueaoAdmin
{

    /**
     * 定义当前操作表名
     * @var string
     */
    public $table = 'MyCate';

    /**
     * 商品分类列表
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '我的分类';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['pid']) && is_numeric($get['pid'])) {
            $db->where('pid', $get['pid']);
            $parent_pid=getmodel($get['pid'],'my_cate','pid');
        }else{
            $db->where('pid', 0);
            $parent_pid=NULL;
        }
        $this->assign('parent_pid',$parent_pid);
        return parent::_list($db->order('sort asc,id asc'));
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_data_filter(&$data)
    {
        foreach ($data as &$vo) {
            $vo['ids'] = join(',', ToolsService::getArrSubIds($data, $vo['id']));
        }
        $data = ToolsService::arr2table($data);
    }

    /**
     * 添加菜单
     * @return array|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        return $this->_form($this->table, 'form','',[],array('pid'=>$this->request->get('pid')));
    }

    /**
     * 表单数据前缀方法
     * @param array $vo
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$vo,$data)
    {
        if ($this->request->isGet()) {
            // 读取上级分类
            $where = ['status' => '1'];
            $_cates = (array)Db::name($this->table)->where($where)->order('sort desc,id desc')->select();
            array_unshift($_cates, ['id' => 0, 'pid' => -1, 'title' => '--- 顶级分类 ---']);
            $cates = ToolsService::arr2table($_cates);
            foreach ($cates as $key => &$cate) {
                if (isset($vo['pid'])&&isset($vo['id'])) {
                    $path = "-{$vo['pid']}-{$vo['id']}";
                    if ($vo['pid'] !== '' && (stripos("{$cate['path']}-", "{$path}-") !== false || $cate['path'] === $path)) {
                        unset($cates[$key]);
                    }
                }
            }
            $this->assign('cates', $cates);
        }
    }

    /**
     * 添加/编辑成功回跳处理
     * @param bool $result
     */
    protected function _form_result($result,$data)
    {
    }

}
